const tools=(function(){
    const getStyle = (obj, attr) => {
        if (window.getComputedStyle) {
            //null 如果获取元素伪元素 :before :after
            return window.getComputedStyle(obj, null)[attr];
        }
        return obj.currentStyle[attr];
    }


    const animate = (oEle, json, callback) => {
        clearInterval(oEle.timer);
        oEle.timer = setInterval(() => {
            let current=0;
            let target=0;
            let steps=0;
            let flag=true;
            for(let attr in json){
                if(attr=="opacity"){
                    target=parseFloat(json[attr])*100;
                    current=parseFloat(getStyle(oEle,attr))*100;
                }else{
                    target=parseInt(json[attr]);
                    current=parseInt(getStyle(oEle,attr));
                }
                steps=(target-current)/10;
                steps=steps>0?Math.ceil(steps):Math.floor(steps);
                //新的位置=当前位置+步长
                if(attr=="opacity"){
                    oEle.style[attr]=(current+steps)/100;
                }else if(attr=="zIndex"){
                    oEle.style[attr]=target;
                }else{
                    oEle.style[attr]=current+steps+"px";
                }
                if(target!=current){
                    flag=false;
                }
            }
            if(flag){
                clearInterval(oEle.timer);
                if(typeof(callback)=="function"){
                    callback();
                }
            }
        }, 20)
    }
    return{
        getStyle,
        animate
    }
})()
//暴露出去